package zjut.raymond.witdemo.ui.adapter;

import android.content.Context;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

import zjut.raymond.witdemo.R;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
    ArrayList<String> list;
    private Context context;
    private OnItemListener onItemLister;

    private int temp = -1;

    public MyAdapter(Context context, ArrayList<String> list) {
        this.context = context;
        if (list != null) {
            this.list = list;
        } else {
            this.list = new ArrayList<>();
        }
    }

    public interface OnItemListener {
        void onClick(View v, int pos, String projectc);
    }

    public void setOnItemLister(OnItemListener onItemLister) {
        this.onItemLister = onItemLister;
    }

    public void setDefSelect(int position) {
        this.temp = position;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
        View itemView = layoutInflater.inflate(R.layout.item_recycleview, parent, false);
        return new MyViewHolder(itemView);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void onBindViewHolder(@NonNull final MyViewHolder holder, final int position) {
        holder.tv.setText(list.get(position));
        if (temp != -1) {
            if (temp == position) {
                holder.imgView.setImageResource(R.drawable.ic_check_box_24);
            } else {
                holder.imgView.setImageResource(R.drawable.ic__check_box_outline_blank_24);
            }
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView tv;
        ImageView imgView;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            tv = itemView.findViewById(R.id.item_tv_ble_info);
            imgView = itemView.findViewById(R.id.item_img_check_out);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onItemLister != null) {
                        onItemLister.onClick(v, getLayoutPosition(), list.get(getLayoutPosition()));
                    }
                }
            });
        }
    }
}
